package org.deltaset.meta.internal;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import org.deltaset.meta.MetaField;
import org.deltaset.meta.annotation.DsCustom;
import org.deltaset.meta.internal.mockmeta.persistence.Column;
import org.deltaset.meta.internal.mockmeta.persistence.ManyToMany;
import org.deltaset.meta.internal.mockmeta.persistence.ManyToOne;
import org.deltaset.meta.internal.mockmeta.persistence.OrderColumn;
import org.deltaset.meta.internal.testvisit.Person;
import org.deltaset.meta.internal.testvisit.UploadedFile;
import static org.junit.Assert.*;

/**
 *
 * @author dmakariev
 */
public final class AnimalHelper {

    private AnimalHelper() {
    }

    public static void assertFormFields(final List<MetaField> formMetaFields) {

        assertEquals("formMetaFields.size ", 6, formMetaFields.size());

        {
            final MetaField metaField = formMetaFields.get(0);

            final String propertyLabelKey = metaField.getPropertyLabelKey();
            assertEquals("metaField getPropertyLabelKey() ", "name", propertyLabelKey);

            final String propertyName = metaField.getPropertyName();
            assertEquals("metaField getPropertyName() ", "name", propertyName);

            final String propertyType = metaField.getPropertyType();
            assertEquals("metaField getPropertyType() ", "string", propertyType);

            final Class originalType = metaField.getOriginalType();
            assertEquals("metaField getOriginalType() ", String.class, originalType);

            final Class genericType = metaField.getGenericType();
            assertEquals("metaField getGenericType() ", null, genericType);

            final Annotation[] annotations = metaField.getOriginalAnnotations();
            assertEquals("metaField getOriginalAnnotations().length ", 0, annotations.length);

            final Map<String, String> params = metaField.getParams();
            assertEquals("metaField getParams().size() ", 0, params.size());
        }

        {
            final MetaField metaField = formMetaFields.get(1);

            final String propertyLabelKey = metaField.getPropertyLabelKey();
            assertEquals("metaField getPropertyLabelKey() ", "origin", propertyLabelKey);

            final String propertyName = metaField.getPropertyName();
            assertEquals("metaField getPropertyName() ", "origin", propertyName);

            final String propertyType = metaField.getPropertyType();
            assertEquals("metaField getPropertyType() ", "textarea", propertyType);

            final Class originalType = metaField.getOriginalType();
            assertEquals("metaField getOriginalType() ", String.class, originalType);

            final Class genericType = metaField.getGenericType();
            assertEquals("metaField getGenericType() ", null, genericType);

            final Annotation[] annotations = metaField.getOriginalAnnotations();
            assertEquals("metaField getOriginalAnnotations().length ", 1, annotations.length);

            final Annotation a0 = annotations[0];
            assertEquals("metaField a0.annotationType() ", DsCustom.class, a0.annotationType());

            //"cols=30", "rows=5"
            final Map<String, String> params = metaField.getParams();
            assertEquals("metaField getParams().size() ", 2, params.size());

            assertTrue("params.containsKey(cols)", params.containsKey("cols"));
            assertEquals("params['cols']=30 ", "30", params.get("cols"));
            assertTrue("params.containsKey(rows)", params.containsKey("rows"));
            assertEquals("params['rows']=5 ", "5", params.get("rows"));
        }

        {
            final MetaField metaField = formMetaFields.get(2);

            final String propertyLabelKey = metaField.getPropertyLabelKey();
            assertEquals("metaField getPropertyLabelKey() ", "note", propertyLabelKey);

            final String propertyName = metaField.getPropertyName();
            assertEquals("metaField getPropertyName() ", "note", propertyName);

            final String propertyType = metaField.getPropertyType();
            assertEquals("metaField getPropertyType() ", "string", propertyType);

            final Class originalType = metaField.getOriginalType();
            assertEquals("metaField getOriginalType() ", String.class, originalType);

            final Class genericType = metaField.getGenericType();
            assertEquals("metaField getGenericType() ", null, genericType);

            final Annotation[] annotations = metaField.getOriginalAnnotations();
            assertEquals("metaField getOriginalAnnotations().length ", 1, annotations.length);
            final Annotation a0 = annotations[0];
            assertEquals("metaField a0.annotationType() ", Column.class, a0.annotationType());

            final Map<String, String> params = metaField.getParams();
            assertEquals("metaField getParams().size() ", 0, params.size());
        }

        {
            final MetaField metaField = formMetaFields.get(3);

            final String propertyLabelKey = metaField.getPropertyLabelKey();
            assertEquals("metaField getPropertyLabelKey() ", "weight", propertyLabelKey);

            final String propertyName = metaField.getPropertyName();
            assertEquals("metaField getPropertyName() ", "weight", propertyName);

            final String propertyType = metaField.getPropertyType();
            assertEquals("metaField getPropertyType() ", "number", propertyType);

            final Class originalType = metaField.getOriginalType();
            assertEquals("metaField getOriginalType() ", Double.class, originalType);

            final Class genericType = metaField.getGenericType();
            assertEquals("metaField getGenericType() ", null, genericType);

            final Annotation[] annotations = metaField.getOriginalAnnotations();
            assertEquals("metaField getOriginalAnnotations().length ", 0, annotations.length);

            final Map<String, String> params = metaField.getParams();
            assertEquals("metaField getParams().size() ", 0, params.size());
        }

        {
            final MetaField metaField = formMetaFields.get(4);

            final String propertyLabelKey = metaField.getPropertyLabelKey();
            assertEquals("metaField getPropertyLabelKey() ", "owner", propertyLabelKey);

            final String propertyName = metaField.getPropertyName();
            assertEquals("metaField getPropertyName() ", "owner", propertyName);

            final String propertyType = metaField.getPropertyType();
            assertEquals("metaField getPropertyType() ", "custom", propertyType);

            final Class originalType = metaField.getOriginalType();
            assertEquals("metaField getOriginalType() ", Person.class, originalType);

            final Class genericType = metaField.getGenericType();
            assertEquals("metaField getGenericType() ", null, genericType);

            final Annotation[] annotations = metaField.getOriginalAnnotations();
            assertEquals("metaField getOriginalAnnotations().length ", 1, annotations.length);

            final Annotation a0 = annotations[0];
            assertEquals("metaField a0.annotationType() ", ManyToOne.class, a0.annotationType());

            final Map<String, String> params = metaField.getParams();
            assertEquals("metaField getParams().size() ", 0, params.size());
        }

        {
            final MetaField metaField = formMetaFields.get(5);

            final String propertyLabelKey = metaField.getPropertyLabelKey();
            assertEquals("metaField getPropertyLabelKey() ", "uploadedFiles", propertyLabelKey);

            final String propertyName = metaField.getPropertyName();
            assertEquals("metaField getPropertyName() ", "uploadedFiles", propertyName);

            final String propertyType = metaField.getPropertyType();
            assertEquals("metaField getPropertyType() ", "collection", propertyType);

            final Class originalType = metaField.getOriginalType();
            assertEquals("metaField getOriginalType() ", List.class, originalType);

            final Class genericType = metaField.getGenericType();
            assertEquals("metaField getGenericType() ", UploadedFile.class, genericType);

            final Annotation[] annotations = metaField.getOriginalAnnotations();
            assertEquals("metaField getOriginalAnnotations().length ", 2, annotations.length);

            final Annotation a0 = annotations[0];
            assertEquals("metaField a0.annotationType() ", ManyToMany.class, a0.annotationType());

            final Annotation a1 = annotations[1];
            assertEquals("metaField a1.annotationType() ", OrderColumn.class, a1.annotationType());

            final Map<String, String> params = metaField.getParams();
            assertEquals("metaField getParams().size() ", 0, params.size());
        }
    }
}
